﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopBLL;
using ShopModel;

public partial class cms_NewsletterEdit : System.Web.UI.Page
{
    #region METHODS

    protected bool _Validate()
    {
        bool result = true;
        this.Validate();

        if (!rfvContent.IsValid || !rfvDescription.IsValid || !rfvTitle.IsValid)
        {
            CtrlShowInfo.AppendError(Resources.Language.FillInRequiredFields);
            result = false;
        }

        if (cDispatchDate.SelectedDate < DateTime.Now)
        {
            CtrlShowInfo.AppendError(Resources.Language.ChooseTheCorrectDate);
            lCalendarError.Visible = true;
            result = false;
        }

        return result;
    }

    protected void RefreshNewsletterList(string SelectValue)
    {
        ddlNewsletters.Items.Clear();

        ddlNewsletters.Items.Add(new ListItem(Resources.Language.NewNews, ""));
        ddlNewsletters.DataSource = ReportsManager.Instance.GetNewsletters(CommonTools.CurrentLanguageID);
        ddlNewsletters.DataBind();

        ddlNewsletters.SelectedValue = SelectValue;

        if (ddlNewsletters.SelectedValue != string.Empty)
        {
            btnDeleteTop.Enabled = true;
            btnDeleteBottom.Enabled = true;
        }
    }

    protected void ClearNewsletterData()
    {
        heContent.Text = string.Empty;
        tbDescription.Text = string.Empty;
        tbTitle.Text = string.Empty;
        lStatus.Text = string.Empty;
    }

    protected void RefreshNewsletterData()
    {
        ShopModel.Newsletter CurrentNewsletter = ReportsManager.Instance.GetNewsletter(int.Parse(ddlNewsletters.SelectedValue), int.Parse(ddlLanguage.SelectedValue));

        if (CurrentNewsletter != null)
        {
            heContent.Text = CurrentNewsletter.Content;
            tbDescription.Text = CurrentNewsletter.Description;
            tbTitle.Text = CurrentNewsletter.Title;
            cDispatchDate.SelectedDate = CurrentNewsletter.NewsDateSend;
            lStatus.Text = CurrentNewsletter.IsSent ? Resources.Language.Sent : Resources.Language.NotSent;
        }
    }

    #endregion METHODS

    #region EVENTS

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlNewsletters.SelectedValue != string.Empty)
            RefreshNewsletterData();
    }

    protected void ddlNews_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlNewsletters.SelectedValue != string.Empty)
            RefreshNewsletterData();
        else
            ClearNewsletterData();
    }

    protected void New_Click(object sender, EventArgs e)
    {
        ClearNewsletterData();
        ddlNewsletters.SelectedIndex = 0;
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        if (_Validate())
        {
            Newsletter news = new Newsletter(tbTitle.Text, tbDescription.Text, heContent.Text, DateTime.Now, cDispatchDate.SelectedDate, false);

            if (ddlNewsletters.SelectedValue != string.Empty)
                news.NewsID = int.Parse(ddlNewsletters.SelectedValue);

            int id = ReportsManager.Instance.SaveNewsletter(news, int.Parse(ddlLanguage.SelectedValue));

            if (id > 0)
                CtrlShowInfo.ShowInfo(false, Resources.Language.cmsMessageDataSaveSuccessful);
            else
                CtrlShowInfo.ShowInfo(true, Resources.Language.cmsMessageDataSaveFailure);

            RefreshNewsletterList(id.ToString());
        }
    }

    protected void Delete_Click(object sender, EventArgs e)
    {
        if (ReportsManager.Instance.DeleteNewsletter(int.Parse(ddlNewsletters.SelectedValue)))
            CtrlShowInfo.ShowInfo(false, Resources.Language.NewsDeleted);
        else
            CtrlShowInfo.ShowInfo(true, Resources.Language.Error_WhileDeleting);

        RefreshNewsletterList(string.Empty);
        ClearNewsletterData();
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RefreshNewsletterList(string.Empty);

            ddlLanguage.DataSource = ReportsManager.Instance.GetAvailableLanguages(true);
            ddlLanguage.DataBind();

            ddlLanguage.SelectedValue = CommonTools.CurrentLanguageID.ToString();
        }
    }

    #endregion PAGE EVENTS
}
